<?php
class FuncoesGerais {
	
	// Recebe um str no formato dd-mm-aaaa e retorna no formato aaaa-mm-dd
	public static function date_to_data_base($data) {
		if(!$data) return null;
		$pos = strpos($data, '-');
		$rpos = strrpos($data, '-');
		return substr($data, $rpos + 1).'-'.substr($data, $pos + 1, $rpos - $pos - 1).'-'.substr($data, 0, $pos);
	}
	// Recebe um str no formato aaaa-mm-dd e retorna no formato dd/mm/aaaa
	public static function data_base_to_date($data) {
		if(!$data) return '';
		return substr($data, strrpos($data, '-') + 1).'-'.substr($data, strpos($data, '-') + 1, strrpos($data, '-') - strpos($data, '-') - 1).'-'.substr($data, 0, strpos($data, '-'));
	}

	public static function random_string_generate($nice = 1, $length=0, $allowchars = "") {
		mt_srand((double)microtime()*1000000);
		# Find random password length
		if (!$length) $length = mt_rand(5, 9);
		# pronouncable password
		if ($nice == 1) return FuncoesGerais::random_string_generate_advanced($length, 1, 1, 1, 0, 0, $allowchars);
		# lowercase only, fix similar
		else if ($nice == 2) return FuncoesGerais::random_string_generate_advanced($length, 0, 1, 0, 0, 1, $allowchars);
		# lowercase and numbers only, fix similar
		else if ($nice == 3) return FuncoesGerais::random_string_generate_advanced($length, 0, 1, 1, 0, 1, $allowchars);
		# both lower and uppercase chars and numbers , fix similar
		else if ($nice == 4) return FuncoesGerais::random_string_generate_advanced($length, 1, 1, 1, 0, 1, $allowchars);
		# all types of letters, including special chars, fix similar
		else if ($nice == 5) return FuncoesGerais::random_string_generate_advanced($length, 1, 1, 1, 1, 1, $allowchars);
		# oh my :) the real deal - get it all and dont fix similars
		else if ($nice == 6) return FuncoesGerais::random_string_generate_advanced($length, 1, 1, 1, 1, 0, $allowchars);

		# $nice contained illegal value, go for the easy 3
		else return FuncoesGerais::random_string_generate_advanced($length, 1, 1, 1, 0, 1);
	}

	private static function random_string_generate_advanced($length = 8, $allow_uppercase = 1, $allow_lowercase = 1, $allow_numbers = 1, $allow_special = 1, $fix_similar = 0, $valid_charset = "") {
		# Create a list of usable chars based upon the parameters
		if (!$valid_charset){
			if ($allow_uppercase) $valid_charset .= 'ABCDEFGHIJKLMNOPQRSTUVXYZ';
			if ($allow_lowercase) $valid_charset .= 'abcdefghijklmnopqrstuvxyz';
			if ($allow_numbers) $valid_charset .= '0123456789';
			if ($allow_special) $valid_charset .= '!#$%&()*+-./;<=>@\_';
		}
		
		# Find the charset length
		$charset_length = strlen($valid_charset);

		# If no chars is allowed, return false
		if ($charset_length == 0) return false;

        # Initialize the password and loop till we have all
		$string = "";
		while(strlen($string) < $length) {
			# Pull out a random char
			$char = $valid_charset[mt_rand(0, ($charset_length-1))];
			
			# If similar is true, check if string contains mistakeable chars, add if accepted
            if (($fix_similar && !strpos('O01lI5S', $char)) || !$fix_similar) $string .= $char;
		}

		# Return password
		return $string;
    }
}
?>